﻿using AuthCenter.API.Application.Command.Role;
using AuthCenter.API.Application.Command.RoleMenu;
using AuthCenter.API.DTOs;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.RoleMenu
{
    public class GetRoleTreeHandler : IRequestHandler<GetRoleTreeCommand, APIResult<List<Tree>>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;

        public GetRoleTreeHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
        }
        public Task<APIResult<List<Tree>>> Handle(GetRoleTreeCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Tree>> result = new APIResult<List<Tree>>();
            var res = roleRepo.GetAll().Where(x=>!x.IsDel).ToList();
            List<Tree> list = new List<Tree>(); 
            foreach (var item in res)
            {
                list.Add(new Tree
                {
                    id = item.Id,
                    label = item.RoleName
                });
            }
            result.Message = "显示成功";
            result.Code = APIEnum.Ok;
            result.Data = list;
            return Task.FromResult(result);
        }
    }
}
